package examstarter;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import studentmanager.GUICourseDisplay;

import exammanager.GUIExamDisplay;
import gui.CustomJButton;

public class GUIStarterLeft extends JPanel{

	private static final long serialVersionUID = 1L;
	private static final String SERVER_CLOSED = "MỞ SERVER";
	private static final String SERVER_OPENED = "TẮT SERVER";
	private GUIStarter parent;
	
	private GUICourseDisplay courseDisplay;
	private GUIExamDisplay examDisplay;
	private JButton btAdd;
	private JButton btOpenServer;
	
	public GUIStarterLeft(GUIStarter parent) {
		this.parent = parent;
		setOpaque(false);
		setLayout(new BorderLayout());
		
		JPanel mainPanel = new JPanel(new GridBagLayout());
		mainPanel.setOpaque(false);
		
		Border border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		
		courseDisplay = new GUICourseDisplay();
		examDisplay = new GUIExamDisplay();
		
		courseDisplay.getJList().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		courseDisplay.getJList().addListSelectionListener(new ExamSelectionListener());
		courseDisplay.getJList().addMouseListener(new MouseListener());
		
		examDisplay.getJList().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		examDisplay.getJList().addListSelectionListener(new ExamSelectionListener());
		examDisplay.getJList().addMouseListener(new MouseListener());
		
		JPanel topleftPanel = new JPanel(new BorderLayout());
		topleftPanel.setOpaque(false);
		topleftPanel.setBorder(BorderFactory.createTitledBorder(border, "Chọn Khoá thi"));
		topleftPanel.add(courseDisplay,BorderLayout.CENTER);
		
		JPanel botleftPanel = new JPanel(new BorderLayout());
		botleftPanel.setOpaque(false);
		botleftPanel.setBorder(BorderFactory.createTitledBorder(border, "Chọn Đề thi"));
		botleftPanel.add(examDisplay,BorderLayout.CENTER);
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx =1f;
		c.weighty = 0.5f;
		mainPanel.add(topleftPanel,c);
		c.gridx = 0;
		c.gridy = 1;
		mainPanel.add(botleftPanel,c);
		
		//button
		JPanel buttonPanel = new JPanel(new BorderLayout());
		buttonPanel.setBorder(new EmptyBorder(0, 3, 0, 3));
		buttonPanel.setOpaque(true);
		btOpenServer = new CustomJButton(SERVER_CLOSED, 120, 30, 
				CustomJButton.LIGHTERBLUE, CustomJButton.LIGHTER,
				Color.darkGray, 2, false, true);
		btOpenServer.addActionListener(new OpenServerAction());
		btAdd = new CustomJButton("Thêm>>>", 120, 30, Color.WHITE, CustomJButton.DARKER,
				Color.darkGray, 2, false, true);
		btAdd.addActionListener(new AddAction());
		btAdd.setEnabled(false);
		
		buttonPanel.add(btOpenServer,BorderLayout.WEST);
		buttonPanel.add(btAdd,BorderLayout.EAST);
		
		add(mainPanel,BorderLayout.CENTER);
		add(buttonPanel,BorderLayout.SOUTH);
		refresh();
	}
	
	public void refresh(){
		examDisplay.getExamFromDB();
		courseDisplay.getCourseFromDB(false);
	}
	
	class OpenServerAction extends AbstractAction{
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(btOpenServer.getText() == SERVER_CLOSED){
				if(parent.startServer()){
					JOptionPane.showMessageDialog(parent, "Mở server thành công.", 
							"Thông báo", JOptionPane.INFORMATION_MESSAGE);
					btOpenServer.setText(SERVER_OPENED);
					btOpenServer.setToolTipText(parent.getIp() + ":" + parent.getPort());
					btAdd.setEnabled(true);
				}else{
					JOptionPane.showMessageDialog(parent, "Mở server không thành công.", 
							"Thông báo", JOptionPane.ERROR_MESSAGE);
				}
			}else if(btOpenServer.getText() == SERVER_OPENED){
				if(parent.stopServer()){
					JOptionPane.showMessageDialog(parent, "Tắt server thành công.", 
							"Thông báo", JOptionPane.INFORMATION_MESSAGE);
					btOpenServer.setText(SERVER_CLOSED);
					btAdd.setEnabled(false);
					
				}else{
					JOptionPane.showMessageDialog(parent, "Tắt server không thành công. Có Khoá đang tiến hành thi.", 
							"Thông báo", JOptionPane.ERROR_MESSAGE);
				}
			}
			
		}
	}
	class AddAction extends AbstractAction{
		
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(courseDisplay.getJList().getSelectedIndex() == -1){
				JOptionPane.showMessageDialog(parent, "Vui lòng chọn Khoá thi trước khi thêm mới", 
						"Thông báo", JOptionPane.WARNING_MESSAGE);
				return;
			}
			
			if(examDisplay.getJList().getSelectedIndex() == -1){
				JOptionPane.showMessageDialog(parent, "Vui lòng chọn Đề thi trước khi thêm mới", 
						"Thông báo", JOptionPane.WARNING_MESSAGE);
				return;
			}
			
			parent.getRightPanel().addItem(courseDisplay.getJList().getSelectedValue(),
					examDisplay.getJList().getSelectedValue());
		}
	}
	
	class MouseListener extends MouseAdapter{

		@Override
		public void mouseClicked(MouseEvent e) {
			switch(e.getModifiers()) {
				case InputEvent.BUTTON1_MASK: {  
					super.mouseClicked(e);	
					break;
		        }
				case InputEvent.BUTTON2_MASK: {    
					break;
		        }
				case InputEvent.BUTTON3_MASK: {
			        break;
		        }
			}
		}
	}
	
	class ExamSelectionListener implements ListSelectionListener{
		
		@Override
		public void valueChanged(ListSelectionEvent e) {
			if(!e.getValueIsAdjusting()){
				if(examDisplay.getJList().equals(e.getSource())){
				}else if(courseDisplay.getJList().equals(e.getSource())){
				}else{
				}
			}
		}
	}
}
